<?php
include './include/common.inc.php';
include './include/workflow.class.php';
include 'form.class.php';

$action = isset($action) ? $action : 'manage';
$site = new site();
$workflow_db = new workflow();
$modules = get_moudule_info();
foreach($modules as $info)
{
    $modules[$info['name']] = $info['title'];
}
$list = $workflow_db->workflow_list();//获取工作流数据
$workflow_list = array('' => '不审核');
if(!empty ($list))
{
    foreach($list as $r)
    {
        $workflow_list[$r['id']] = $r['name'];
    }
}

switch ($action)
{
    case 'add' :
        if(isset($dosubmit))
        {
            $site->set($data);
            $errors = $site->validate();
            //校验是否通过
            if(!empty($errors))
            {
                foreach($errors as $field => $error)
                {
                    $response->text_alert('error', $field, $error);
                }
                $response->real_execute();
            }
            else
            {
                $orginal = $site->get_one($data['id']);
                //站点已经存在
                if(!empty($orginal))
                {
                    $response->text_alert('error', 'id', '该站点编号已经存在');
                    $response->real_execute();
                }
                $result = $site->save();
                if($result)
                {
                    $response->alert('添加站点成功！');
                    $response->redirect($forward);
                }
                else
                {
                    $response->alert('站点添加失败');
                }
                $response->execute();
            }
        }
        else
        {
            include template('admin', 'site_add');
        }

        break;

    case 'edit' : 

        if(isset($dosubmit))
        {
            $site->set($data);
            $errors = $site->validate();
            if(!empty($errors))
            {
                foreach($errors as $field => $error)
                {
                    $response->text_alert('error', $field, $error);
                }
                $response->real_execute();
            }
            else
            {
                $result = $site->save();
                if($result)
                {
                    $response->alert('修改站点成功！');
                    $response->redirect($forward);
                }
                else
                {
                    $response->alert('站点修改失败');
                }
                $response->execute();
            }
        }
        else
        {
            if(isset($id))
            {
                $data= $site->get_one($id);
            }
            include template('admin', 'site_add');
        }
        break;

    case 'delete' :

        if(!isset($id))
        {
            raise404();
        }
        if(!$site->check_site_contents($id))
        {
            $response->alert("站点非空，不能删除");
            $response->real_execute();
        }
        $result = $site->delete($id);
        if($result)
        {
            $response->alert('站点删除成功');
            $response->script('window.location=window.location');
        }
        else
        {
            $response->alert('站点删除失败');
        }
        $response->execute();
        break;

    case 'flush' :

        if(!isset($id))
        {
            raise404();
        }

        $result = $site->flush_site_contents($id);
        if($result)
        {
            $response->alert('站点数据清除成功');
            $response->script('window.location=window.location');
        }
        else
        {
            $response->alert('站点数据清除失败');
        }
        $response->execute();
        break;

    case 'export' :
        
        if(!isset($id))
        {
            raise404();
        }
        $site->export($id);

        break;

    

    case 'sync' :

        if(!isset($id))
        {
            raise404();
        }
        $data = $site->get_one($id);
        $site->set($data);
        $result = $site->copy_code();
//        $result = $site->make_config();
        if($result)
        {
            $response->alert('代码同步成功');
            $response->script('window.location=window.location');
        }
        else
        {
            $response->alert('代码同步失败');
        }
        $response->execute();
        break;

    case 'cache' :

        if(!isset($id))
        {
            raise404();
        }
        $data = $site->get_one($id);
        $site->set($data);
        $result = $site->make_cache();
        if($result)
        {
            $response->alert('缓存生成成功');
            $response->script('window.location=window.location');
        }
        else
        {
            $response->alert('缓存生成失败');
        }
        $response->execute();
        break;

    case 'import' :

        if(isset($dosubmit))
        {
            $result = $site->import();
            if($result)
            {
                showmessage('导入成功', $forward);
            }
            else
            {
                showmessage('导入失败', $forward);
            }
        }
        else
        {
            include template('admin', 'site_import');
        }
        break;

    default :
        $data = $site->all();
        include template('admin', 'site_manage');

}
?>
